/**
  ******************************************************************************
  * File Name          : CAN.c
  * Description        : This file provides code for the configuration
  *                      of the CAN instances.
  ******************************************************************************
  ** This notice applies to any and all portions of this file
  * that are not between comment pairs USER CODE BEGIN and
  * USER CODE END. Other portions of this file, whether 
  * inserted by the user or by software development tools
  * are owned by their respective copyright owners.
  *
  * COPYRIGHT(c) 2019 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "can.h"

#include "gpio.h"

/* USER CODE BEGIN 0 */
#include "sys.h"
u8 can_1_buf[8];
u8 databuf[8]= {0};
u8 databuf_PTZ[8]= {0,0,0,0,0,0,0,0};
u8 GM_dat1[8]= {0};
u8 GM_dat2[8]= {0};

/* USER CODE END 0 */

CAN_HandleTypeDef hcan1;

/* CAN1 init function */
void MX_CAN1_Init(void)
{

  hcan1.Instance = CAN1;
  hcan1.Init.Prescaler = 9;
  hcan1.Init.Mode = CAN_MODE_NORMAL;
  hcan1.Init.SJW = CAN_SJW_1TQ;
  hcan1.Init.BS1 = CAN_BS1_2TQ;
  hcan1.Init.BS2 = CAN_BS2_2TQ;
  hcan1.Init.TTCM = DISABLE;
  hcan1.Init.ABOM = DISABLE;
  hcan1.Init.AWUM = DISABLE;
  hcan1.Init.NART = DISABLE;
  hcan1.Init.RFLM = DISABLE;
  hcan1.Init.TXFP = ENABLE;
  if (HAL_CAN_Init(&hcan1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(canHandle->Instance==CAN1)
  {
  /* USER CODE BEGIN CAN1_MspInit 0 */

  /* USER CODE END CAN1_MspInit 0 */
    /* CAN1 clock enable */
    __HAL_RCC_CAN1_CLK_ENABLE();
  
    /**CAN1 GPIO Configuration    
    PD0     ------> CAN1_RX
    PD1     ------> CAN1_TX 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

    /* CAN1 interrupt Init */
    HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 2, 1);
    HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);
  /* USER CODE BEGIN CAN1_MspInit 1 */
        static CanTxMsgTypeDef  Tx1Message;
        static CanRxMsgTypeDef  Rx1Message;
        CAN_FilterConfTypeDef can_filter;
        can_filter.FilterNumber=0;
        can_filter.FilterMode=CAN_FILTERMODE_IDMASK;
        can_filter.FilterScale=CAN_FILTERSCALE_32BIT;
        can_filter.FilterIdHigh=0x0000;
        can_filter.FilterIdLow=0x0000;
        can_filter.FilterMaskIdHigh=0x0000;
        can_filter.FilterMaskIdLow=0x0000;
        can_filter.FilterFIFOAssignment=0;
        can_filter.FilterActivation=ENABLE;
        can_filter.BankNumber = 14;
        /*can1和CAN2使用不同的滤波器*/
        can_filter.FilterNumber = 0;
        (&hcan1)->pTxMsg = &Tx1Message;
        (&hcan1)->pRxMsg = &Rx1Message;
        HAL_CAN_ConfigFilter(&hcan1, &can_filter);
        __HAL_CAN_ENABLE_IT(&hcan1,CAN_IT_FMP0);
  /* USER CODE END CAN1_MspInit 1 */
  }
}

void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
{

  if(canHandle->Instance==CAN1)
  {
  /* USER CODE BEGIN CAN1_MspDeInit 0 */

  /* USER CODE END CAN1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_CAN1_CLK_DISABLE();
  
    /**CAN1 GPIO Configuration    
    PD0     ------> CAN1_RX
    PD1     ------> CAN1_TX 
    */
    HAL_GPIO_DeInit(GPIOD, GPIO_PIN_0|GPIO_PIN_1);

    /* CAN1 interrupt Deinit */
    HAL_NVIC_DisableIRQ(CAN1_RX0_IRQn);
  /* USER CODE BEGIN CAN1_MspDeInit 1 */

//	__HAL_CAN_ENABLE_IT(&hcan1,CAN_IT_TME);
  /* USER CODE END CAN1_MspDeInit 1 */
  }
} 

/* USER CODE BEGIN 1 */
u8 CAN1_Send_Msg(u8* msg)    //ID´Ó201-204
{
    u16 i=0;
    hcan1.pTxMsg->StdId=0x200;        //±ê×¼±êÊ¶·û
    hcan1.pTxMsg->IDE=CAN_ID_STD;    //Ê¹ÓÃ±ê×¼Ö¡
    hcan1.pTxMsg->RTR=CAN_RTR_DATA;  //Êý¾ÝÖ¡
    hcan1.pTxMsg->DLC=0x08;
    for(i=0; i<8; i++)
    {
        hcan1.pTxMsg->Data[i]=msg[i];
    }
    if(HAL_CAN_Transmit(&hcan1,10)!=HAL_OK) return 1;     //·¢ËÍ
    return 0;
}

u8 CAN1_Send_Msg_PTZ(u8* msg)     //205-207
{
    u16 i=0;
    hcan1.pTxMsg->StdId=0x1FF;        //±ê×¼±êÊ¶·û
//	hcan1.pTxMsg->StdId=0x3F0;
    hcan1.pTxMsg->IDE=CAN_ID_STD;    //Ê¹ÓÃ±ê×¼Ö¡
    hcan1.pTxMsg->RTR=CAN_RTR_DATA;  //Êý¾ÝÖ¡
    hcan1.pTxMsg->DLC=0x08;
    for(i=0; i<8; i++)
    {
        hcan1.pTxMsg->Data[i]=msg[i];
    }
    if(HAL_CAN_Transmit(&hcan1,10)!=HAL_OK) return 1;     //·¢ËÍ
    return 0;
}

u8 CAN1_Receive_Msg(u8 *buf)      //
{
    u32 i;
    if(HAL_CAN_Receive(&hcan1,CAN_FIFO0,0)!=HAL_OK) return 0;//½ÓÊÕÊý¾Ý£¬³¬Ê±Ê±¼äÉèÖÃÎª0
    for(i=0; i<hcan1.pRxMsg->DLC; i++)
        buf[i]=hcan1.pRxMsg->Data[i];
    return hcan1.pRxMsg->DLC;
}
/* USER CODE END 1 */

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
